package main

type ListNode struct {
	Val  int
	Next *ListNode
}

func main() {
	head := &ListNode{1, &ListNode{2, &ListNode{3, &ListNode{4, &ListNode{5, nil}}}}}
	removeNthFromEnd(head, 2)
}

func removeNthFromEnd(head *ListNode, n int) *ListNode {
	slow := head
	fast := head
	for ; n >= 0; n-- {
		fast = fast.Next
	}
	for fast != nil {
		slow = slow.Next
		fast = fast.Next
	}
	slow.Next = slow.Next.Next
	return head
}
